package com.kgcx.flight.controller;

import com.kgcx.common.bean.RequestData;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.constant.ResultCode;
import com.kgcx.common.controller.BaseController;
import com.kgcx.common.utils.http.HttpUtils;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.flight.domain.FlightInfo;
import com.kgcx.flight.service.Impl.AvatarFlightService;
import com.kgcx.flight.service.Impl.LZFlightService;
import com.kgcx.flight.service.Impl.VariFlightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.MD5;

import java.net.URLDecoder;
import java.security.MessageDigest;
import java.util.SortedMap;
import java.util.TreeMap;
import java.security.MessageDigest.*;
import java.util.stream.Stream;

@Api(description = "航班接口", tags = "航班接口")
@RestController
@RequestMapping("flight")
public class FlightController extends BaseController {
    private static final Logger LOG = LoggerFactory.getLogger(FlightController.class);

    @Autowired
    private LZFlightService flightService;
    @Autowired
    private StringRedisTemplate strRedisTemplate;

    @ApiOperation(value = "查询航班", httpMethod = "GET", notes = "查询航班")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fnum", value = "订单号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "date", value = "日期", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("flight")
    public ReturnData<String> queryFlight(@RequestParam(required = true) String fnum,
                                          @RequestParam(required = true) String date) {
        return flightService.QueryFlight(fnum, date);
    }


    @ApiOperation(value = "订制航班推送", httpMethod = "GET", notes = "订制航班推送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fnum", value = "订单号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "date", value = "日期", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dep", value = "出发地机场三字码", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "arr", value = "目的地机场三字码", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("booking")
    public ReturnData<String> booking(@RequestParam(required = true) String fnum,
                                      @RequestParam(required = true) String date,
                                      @RequestParam(required = true) String dep,
                                      @RequestParam(required = true) String arr) {
        return flightService.Booking(fnum, date, dep, arr);
    }


    @ApiOperation(value = "接收航班信息推送", httpMethod = "POST", notes = "接收航班信息推送")
    @PostMapping("listener")
    public ReturnData<String> listener(@RequestBody String content) {
        RequestData rqt = this.getRequestData();
        rqt.values().stream().forEach(obj->LOG.info(JsonUtil.toString(obj) + " ----------\r\n "));

        return flightService.Listener(content);
    }

}
